package com.bjsxt.shop.back.springmvc.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bjsxt.shop.common.controller.BaseController;
import com.bjsxt.shop.goods.service.IStatService;

/**
 * 统计报表业务
 * 
 * @author WYN
 *
 */
@Controller
@RequestMapping("/back/stat/")
public class StatBackController extends BaseController {
	@Resource
	private IStatService statService;
	

	// 先展示商品列表
	@RequestMapping("/goodsList")
	public String goodsList(String st, String ed, Model model,HttpServletRequest request,HttpServletResponse response) throws IOException {
		//读取参数信息,调用商品列表JSON格式
		this.goodsListJSON(request,st, ed, model);
		//根据前台的传的参数判断什么时候是查询数据，什么是下载数据
		String operType=request.getParameter("operType");
		//判断是否是下载数据
		if("downFile".equalsIgnoreCase(operType)){
			/*下载数据，获取数据*/
			List<Map> statList = (List<Map>) request.getAttribute("statList");
			//拼装数据
			List<List<String>> dataList = new ArrayList<List<String>>();
			List<String> titleList = new ArrayList<String>();
			titleList.add("添加时间");
			titleList.add("数量");
			dataList.add(titleList);
			//将数据存放
			for (Iterator iterator = statList.iterator(); iterator.hasNext();) {
				Map map = (Map) iterator.next();
				//每一行的数据
				List<String> rowList = new ArrayList<String>();
				rowList.add(map.get("createTime") + "");
				rowList.add(map.get("count") + "");
				dataList.add(rowList);
			}
			/* 请求响应，下载数据 */
			response.reset();
			response.setHeader("Content-disposition","attachment;filename=1.xls");
			this.poiUtil.writeFile(dataList, response.getOutputStream());
			return null;
		}
		return "/back/stat/goodsList" ;
		
	}
	
	@RequestMapping(value = "/goodsListJSON",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String goodsListJSON(HttpServletRequest request,String st,String ed,Model model){
		JSONObject resultJSON = new JSONObject() ; 
		//先对前台传过来的参数信息
		if(st == null)
		{
			st = "" ; 
		}
		if(ed == null)
		{
			ed = ""; 
		}
		
		Date stDate = null ; 
		Date edDate = null ; 
		if(!"".equalsIgnoreCase(st) && !"".equalsIgnoreCase(ed))
		{
			stDate = this.dateFormatUtil.strDateTime(st);
			edDate = this.dateFormatUtil.strDateTime(ed);
		}
		Map<String, Object> condMap=new HashMap<String, Object>();
		//需要把两个时间参数信息放进去
		condMap.put("stDate",stDate);
		condMap.put("edDate",edDate);
		List<Map>statList=this.statService.statGoodsMapService(condMap);
		model.addAttribute("statList", statList);
		request.setAttribute("statList", statList);
		model.addAttribute("st", st);
		model.addAttribute("ed", ed);
		resultJSON.put("total", statList.size());
		JSONArray rowsArr = new JSONArray() ; 
		/* 拼装json */
		for (Iterator iterator = statList.iterator(); iterator.hasNext();)
		{
			Map map = (Map) iterator.next();
			/* 数据项 */
			JSONObject rowJSON = new JSONObject() ; 
			rowJSON.put("createTime", map.get("createTime"));
			rowJSON.put("count", map.get("count"));
			
			rowsArr.add(rowJSON);
		}
		resultJSON.put("rows", rowsArr);
		return resultJSON.toJSONString();
		
	}
	
}
